<?php
class MailController extends Controller {
	public function index() {
		$this->getSmarty ();
		$this->smarty->display ( "admin/mail.tpl" );
	}
	public function sendMail() { // 页码
		require_once COMMON . DS . 'SendMailUtil.class.php';
		$adress = $_POST ["address"];
		$title = $_POST ["title"];
		$content = $_POST ["content"];
		$sendstatus = SendMailUtil::sendmail ( $title, $content, $adress );
		echo $sendstatus;
	}
	
	public function addOrModify() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'SendMailService.class.php';
		// START 数据库查询及分页数据
		$service = new SendMailService( $this->getDB () );
		if (! CommonBase::isPost ()) {
			$this->smarty->display ( "admin/mail.tpl" );
			return;
		}
		$post =  empty($_POST ['port'] ) ?   25:intval ( $_POST ['port'] );
		$id = $_POST ["id"];
		if (empty ( $id )) {
			$input_data = array (
					'id' => md5 ( uniqid () ),
					'name' => $_POST ["name"],
					'desc' => $_POST ["desc"],
					'password' => $_POST ["password"],
					'smtp' => $_POST ["smtp"],
					'port' => $post,
					'count_total' => $_POST ["count_total"],
					'time' => date('Y-m-d'),
					'sitename' => $_POST ["sitename"]
			);
			$data = $service->addItem ( $input_data );
		} else {
				
			$input_data = array (
					'name' => $_POST ["name"],
					'desc' => $_POST ["desc"],
					'password' => $_POST ["password"],
					'smtp' => $_POST ["smtp"],
					'count_total' => $_POST ["count_total"],
					'port' => $post,
					'sitename' => $_POST ["sitename"]
			);
				
			$input_condition = array (
					'id' => $_POST ["id"]
			);
			$data = $service->edit ( $input_data, $input_condition );
		}
		echo "1";
	}
	function delete() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'SendMailService.class.php';
		// START 数据库查询及分页数据
		$service = new SendMailService( $this->getDB () );
		$id =trim ($_POST ['id']);
		$service->removePosts($id);
		echo -1;
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'SendMailService.class.php';
		// START 数据库查询及分页数据
		$service = new SendMailService( $this->getDB () );
		$service->resetCurDayMail();
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$userid =trim ($_POST ['userid']);
		$offset = ($page - 1) * $rows;
		$result = array ();
		$row_count = $service->countNum ();
		$result ["total"] = $row_count;
		$resource_infos = $service->resourcePage ( $offset, $rows);
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
}

?>